package com.testmock;

import org.apache.velocity.VelocityContext;
import org.mockserver.client.ForwardChainExpectation;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.mock.action.ExpectationResponseCallback;
import org.mockserver.model.*;

import java.util.concurrent.TimeUnit;

import static org.mockserver.model.Header.header;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static org.mockserver.model.HttpStatusCode.*;
import static org.mockserver.model.HttpTemplate.template;
import static org.mockserver.model.Parameter.param;

public class MockResponseServer {
    public static void main(String[] args) {
        startSvr();
    }


    private static void startSvr() {
        ClientAndServer server = new ClientAndServer(1100);
        server.when(
                request().withMethod("POST")
                        .withPath("/tppp")
        )
                .respond(
                        new ExpectationResponseCallback() {
                            public HttpResponse handle(HttpRequest request) {
                                try {
                                    String reqstr = DataHelper.paraXml(request.getBodyAsString(),"//Contr_Num");
                                    VelocityContext context = new VelocityContext();
                                    context.put("rand21", DataHelper.generateRand21());
                                    context.put("contrNum",reqstr);
                                    return response()
                                            .withStatusCode(OK_200.code())
                                            .withHeaders(
                                                    header("Content-Type", "application/soap+xml;charset=UTF-8"),
                                                    header("Accept-Encoding", "gzip, deflate"),
                                                    header("Connection", "Keep-Alive")
                                            )
                                            .withBody(VelocityHelper.generateXML("test.vm", context));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    return response()
                                            .withStatusCode(INTERNAL_SERVER_ERROR_500.code());


                                }
                            }

                        }
                );

    }




}
